<?php

namespace ticket\modules\backend\models;

use Yii;

/**
 * This is the model class for table "tk_ticket_addional_attr".
 *
 * @property int $id
 * @property int $tid
 * @property string $attr_name
 * @property string $attr_ename
 * @property int $attr_type
 * @property string $attr_value
 * @property string $default_val
 * @property string $state
 * @property string $create_time
 */
class TicketAddionalAttr extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_ticket_addional_attr';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            ['tid','required','message' => '门票字段不能为空'],
            ['attr_name','required','message' => '字段名称不能为空'],
            ['attr_ename','required','message' => '字段英文不能为空'],
            ['attr_type','required','message' => '字段类型不能为空'],
            [['tid', 'attr_type'], 'integer'],
            [['attr_value', 'state'], 'string'],
            [['create_time'], 'safe'],
            [['attr_name', 'attr_ename'], 'string', 'max' => 32],
            [['default_val'], 'string', 'max' => 1024],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'tid' => 'Tid',
            'attr_name' => 'Attr Name',
            'attr_ename' => 'Attr Ename',
            'attr_type' => 'Attr Type',
            'attr_value' => 'Attr Value',
            'default_val' => 'Default Val',
            'state' => 'State',
            'create_time' => 'Create Time',
        ];
    }

    /*** 添加修改自定义门票字段信息
     * @param $tid
     * @param $params
     * @return bool
     */
    public function addInfo($tid,$params)
    {
        $attrList = self::find()->where('tid = :id and state = :ste',[':id' => $tid,':ste' => 1])->asArray()->all();
        if (empty($attrList)){
            return true;
        }
        foreach ($attrList as $key => $val){
            if (isset($params[$val['attr_ename']])){
                self::updateAll(['attr_value' => $params[$val['attr_ename']]],['id' => $val['id']]);
            }
        }
        return true;

    }

    /*** 添加自定义字段
     * @param $params
     * @return bool
     */
    public function addAttr($params)
    {
        $data = $this->setData($params);
        if ($this->load($data) and $this->validate()){
            if ($this->save(0)){
                return true;
            }
        }
        return false;
    }

    /*** 修改自定义字段
     * @param $id
     * @param $params
     * @return bool
     */
    public function modAttr($id,$params)
    {
        $data = $this->setData($params);
        if  ($this->load($data) and $this->validate()){
            $model = self::find()->where('id = :id',[':id' => $id])->one();
            if (!is_null($model)){
                $model->tid = $data['TicketAddionalAttr']['tid'];
                $model->attr_name = $data['TicketAddionalAttr']['attr_name'];
                $model->attr_ename = $data['TicketAddionalAttr']['attr_ename'];
                $model->attr_type = $data['TicketAddionalAttr']['attr_type'];
                $model->default_val = $data['TicketAddionalAttr']['default_val'];
                $model->state = isset($data['TicketAddionalAttr']['state'])?$data['TicketAddionalAttr']['state']:'1';
                if  ($model->save(0)){
                    return true;
                }
            }
        }
        return false;
    }


    /*** 删除自定义字段
     * @param $id
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function delTicketAttr($id)
    {
        $model = self::find()->where('id = :id',[':id' => $id])->one();
        if  (!is_null($model)){
            if ($model->delete()){
                return true;
            }
        }
        return false;
    }

    /*** 获取自定义字段信息
     * @param $id
     * @return array|null|\yii\db\ActiveRecord
     */
    public function getAttrInfo($id)
    {
        $data = self::find()->where('id = :id',[':id' => $id])->asArray()->one();
        return $data;
    }



    /*** 删除门票的同时，删除该门票的自定义字段
     * @param $tid
     * @return bool
     */
    public function delTicketAdditionalAttr($tid)
    {
        if (!empty($tid) and is_numeric($tid)){
            if (self::deleteAll(['tid' => $tid])){
                return true;
            }
        }
        return false;
    }

    public function getTicketAttrInfo($tid)
    {
        $data = self::find()->where('tid = :id',[':id' => $tid])->asArray()->all();
        return $data;
    }



    private function setData($params)
    {
        if (isset($params['tid']) and !empty($params['tid'])){
            $data['TicketAddionalAttr']['tid'] = $params['tid'];
        }else{
            $data['TicketAddionalAttr']['tid'] = '';
        }
        if (isset($params['attr_name']) and !empty($params['attr_name'])){
            $data['TicketAddionalAttr']['attr_name'] = $params['attr_name'];
        }else{
            $data['TicketAddionalAttr']['attr_name'] = '';
        }
        if (isset($params['attr_ename']) and !empty($params['attr_ename'])){
            $data['TicketAddionalAttr']['attr_ename'] = $params['attr_ename'];
        }else{
            $data['TicketAddionalAttr']['attr_ename'] = '';
        }
        if (isset($params['attr_type']) and !empty($params['attr_type'])){
            $data['TicketAddionalAttr']['attr_type'] = $params['attr_type'];
        }else{
            $data['TicketAddionalAttr']['attr_type'] = '';
        }
        if (isset($params['default_val']) and !empty($params['default_val'])){
            $data['TicketAddionalAttr']['default_val'] = $params['default_val'];
        }else{
            $data['TicketAddionalAttr']['default_val'] = '';
        }
        if (isset($params['state']) and !empty($params['state'])){
            $data['TicketAddionalAttr']['state'] = $params['state'];
        }else{
            $data['TicketAddionalAttr']['state'] = '1';
        }
        if (!empty($params['attr_value'])){
            $attr_value = explode('\n\r',$params['attr_value']);
        }

        if (isset($params['attr_value']) and !empty($params['attr_value'])){
            $data['TicketAddionalAttr']['attr_value'] = json_encode($attr_value,JSON_UNESCAPED_UNICODE);
        }else{
            $data['TicketAddionalAttr']['attr_value'] = '';
        }

        return $data;
    }

}
